package com.atguigu.gulimall.cart.vo;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车
 * <p>
 * 需要计算的属性，必须重写get方法，保证每次获取属性都会进行计算
 */
@Data
public class Cart {
    List<CartItem> items;

    //    private Integer countNumber;//商品总共数量
    //    private Integer countType;//商品类别数量
    //    private BigDecimal totalAmount;//商品总价

    private BigDecimal reduce = BigDecimal.ZERO;//减免价格

    //获取商品总共数量
    public Integer getCountNumber() {
        int countNumber = 0;
        if (items != null && !items.isEmpty()) {
            for (CartItem item : items) {
                countNumber += item.getCount();
            }
        }
        return countNumber;
    }

    //获取商品类别数量
    public Integer getCountType() {
        int countType = 0;
        if (items != null && !items.isEmpty()) {
            countType = items.size();
        }
        return countType;
    }

    //获取商品总价
    public BigDecimal getTotalAmount() {
        BigDecimal totalAmount = BigDecimal.ZERO;
        //计算购物项总价
        if (items != null && !items.isEmpty()) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    totalAmount = totalAmount.add(totalPrice);
                }
            }
        }
        //减去优惠总价
        totalAmount = totalAmount.subtract(this.getReduce());
        return totalAmount;
    }
}
